package com.efounder.frame.title;

import android.content.Context;
import androidx.core.content.res.ResourcesCompat;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.efounder.mobilemanager.R;

/**
 * Created by hudq on 2016/8/16. XX
 */

public class EFSimpleTitleView extends EFTitleView {
    private ImageView leftImageView;
    private ImageView rightImageView;
    private ImageView filterView;

    public EFSimpleTitleView(Context context) {
        super(context);
        //父布局已经设置颜色了，我们这里就不在设置了 yqs
//        this.setBackgroundResource(R.color.title_Background);
    }

    @Override
    public void initLeftLayout(LinearLayout leftLayout) {
        //返回按钮
        leftImageView = new ImageView(leftLayout.getContext());
        leftImageView.setImageResource(R.drawable.ef_title_view_back);
        //这个别动
        leftImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        leftImageView.setClickable(true);
        //添加背景选择器
        leftImageView.setBackgroundResource(R.drawable.ef_selector_transparent_background);
        int width = (int) getResources().getDimension(R.dimen.ef_title_left_view_height);
        int height = (int) getResources().getDimension(R.dimen.ef_title_left_view_height);
        LinearLayout.LayoutParams backViewParams = new LinearLayout.LayoutParams(width, height);
        leftImageView.setLayoutParams(backViewParams);
        //leftImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        leftLayout.addView(leftImageView);
        //加分割线
        View dividerView = new View(leftLayout.getContext());
        dividerView.setBackgroundResource(R.color.title_Background);
        LinearLayout.LayoutParams dividerViewParam = new LinearLayout.LayoutParams((int) getResources().getDimension(R.dimen.ef_title_left_divider_view_width), ViewGroup.LayoutParams.MATCH_PARENT);
        dividerViewParam.topMargin = (int) getResources().getDimension(R.dimen.ef_title_left_divider_view_margin_vertical);
        dividerViewParam.bottomMargin = (int) getResources().getDimension(R.dimen.ef_title_left_divider_view_margin_vertical);
//        dividerViewParam.leftMargin = (int) getResources().getDimension(R.dimen.ef_title_left_divider_view_margin_horizontal);
        dividerViewParam.leftMargin = (int) getResources().getDimension(R.dimen.ef_title_left_divider_view_margin_horizontal);
        dividerView.setLayoutParams(dividerViewParam);
        leftLayout.addView(dividerView);
    }

    @Override
    public void initCenterLayout(LinearLayout centerLayout) {
        TextView textView = new TextView(centerLayout.getContext());
        textView.setText("标题");
//        textView.setTextSize(getResources().getDimension(R.dimen.ef_title_center_view_text_size));
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.ef_title_center_view_text_size));
        textView.setTextColor(ResourcesCompat.getColor(getResources(), R.color.title_TextColor, null));
        textView.setGravity(Gravity.CENTER);
        int width = ViewGroup.LayoutParams.MATCH_PARENT;
        int height = ViewGroup.LayoutParams.WRAP_CONTENT;
        LinearLayout.LayoutParams centerParams = new LinearLayout.LayoutParams(width, height);
        centerParams.leftMargin = (int) getResources().getDimension(R.dimen.ef_title_left_divider_view_margin_vertical);
        centerParams.rightMargin = (int) getResources().getDimension(R.dimen.ef_title_center_view_margin_horizontal);
        textView.setLayoutParams(centerParams);
        //只显示一行，过长显示...
        textView.setMaxLines(1);
        textView.setEllipsize(TextUtils.TruncateAt.END);
        centerLayout.addView(textView);
    }

    @Override
    public void initRightLayout(LinearLayout rightLayout) {
        //右侧加号按钮
        rightImageView = new ImageView(rightLayout.getContext());
        rightImageView.setImageResource(R.drawable.ef_title_view_add);
        rightImageView.setClickable(true);
        //添加背景选择器
        rightImageView.setBackgroundResource(R.drawable.ef_selector_transparent_background);
        int width = (int) getResources().getDimension(R.dimen.ef_title_right_view_width);
        int height = (int) getResources().getDimension(R.dimen.ef_title_right_view_height);
        LinearLayout.LayoutParams rightImageViewParams = new LinearLayout.LayoutParams(width, height);
        rightImageView.setLayoutParams(rightImageViewParams);
        rightLayout.addView(rightImageView);

//        过滤
        filterView = new ImageView(rightLayout.getContext());
        filterView.setImageResource(R.drawable.manager_icon_search);
        filterView.setClickable(true);
        //添加背景选择器
        //filterView.setImageResource(R.drawable.ef_selector_transparent_background);
        int widthd = (int) getResources().getDimension(R.dimen.ef_title_right_view_width);
        int heightd = (int) getResources().getDimension(R.dimen.ef_title_right_view_height);
        LinearLayout.LayoutParams rightImageViewParamsd = new LinearLayout.LayoutParams(widthd, heightd);
        filterView.setLayoutParams(rightImageViewParamsd);
        filterView.setVisibility(GONE);
        rightLayout.addView(filterView);
    }

    public ImageView getLeftImageView() {
        return leftImageView;
    }

    public ImageView getRightImageView() {
        return rightImageView;
    }

    public ImageView getFilterImageView() {
        return filterView;
    }

    public View getCenterView() {
        return getCenterLayout().getChildAt(0);
    }
}
